package oppsa.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import oppsa.controller.Translate;
import oppsa.model.ActionTypeDB;

import org.jdesktop.swingx.JXFrame;

public class AddActionTypeBox extends JXFrame {
	private static final long serialVersionUID = 1L;
	private JTextField textFieldActionType;
	private JPanel panel;
	private JLabel lblAddActionType;
	private JLabel lblActionType;
	private JButton btnSubmit;
	private ActionTypeDB listActionType;
	private JButton source;
	private JButton btnCancel;
	private ActionDetailsView adv;

	public AddActionTypeBox(final ActionDetailsView adv) {
		addWindowListener(closeWindow);
		setIconImage(Toolkit.getDefaultToolkit().getImage(AddActionTypeBox.class.getResource("/LOGO_FONCE.png")));
		this.adv = adv;
		this.listActionType = new ActionTypeDB();
		final Translate t = new Translate();
		adv.addWindowFocusListener(clickhomeview);
		setStartPosition(StartPosition.CenterInScreen);
		getRootPaneExt().getContentPane().setBackground(Color.RED);
		setMinimumSize(new Dimension(400, 200));
		setPreferredSize(new Dimension(400, 200));
		setResizable(false);
		setTitle(t.getLocalizedText("tlAddActionType"));

		panel = new JPanel();
		panel.setBackground(Color.WHITE);
		getRootPaneExt().getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new MigLayout("", "[][grow]", "[][grow][][][][grow]"));

		lblAddActionType = new JLabel(t.getLocalizedText("lblAddActionType"));
		lblAddActionType.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel.add(lblAddActionType, "cell 0 0");
				
						lblActionType = new JLabel(t.getLocalizedText("lblActionType"));
						lblActionType.setFont(new Font("Tahoma", Font.BOLD, 11));
						panel.add(lblActionType, "cell 0 2,alignx left");
		
				textFieldActionType = new JTextField();
				panel.add(textFieldActionType, "cell 1 2,alignx left");
				textFieldActionType.setColumns(20);

		btnSubmit = new JButton("");
		btnSubmit.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Submit"))));
		btnSubmit.setBorder(null); 
		btnSubmit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				if (textFieldActionType.getText().equals("")) {
					textFieldActionType.setBackground(Color.RED);
					JOptionPane.showMessageDialog(AddActionTypeBox.this, "Aucune valeur saisie");
				} else {
					listActionType = new ActionTypeDB();
					listActionType.insert(textFieldActionType.getText());
					setVisible(false);
					adv.setVisible(true);
					adv.setEnabled(true);
					adv.toFront();
					AddActionTypeBox.this.adv.updateActionType(textFieldActionType.getText());
				}
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("SubmitS"))));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Submit"))));
			}
		});
		panel.add(btnSubmit, "flowx,cell 1 4");

		btnCancel = new JButton("");
		btnCancel.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Cancel"))));
		btnCancel.setBorder(null);
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				adv.setVisible(true);
				adv.setEnabled(true);
				setVisible(false);
				adv.toFront();
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("CancelS"))));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Cancel"))));
			}
		});
		
		
		

		panel.add(btnCancel, "cell 1 4");
	}
	WindowAdapter closeWindow = new WindowAdapter() {
		@Override
		public void windowClosing(WindowEvent arg0) {
			
			setVisible(false);
			adv.setVisible(true);
			adv.setEnabled(true);
			adv.toFront();
		}
	};
	
	WindowFocusListener  clickhomeview = new  WindowFocusListener () {

			@Override
			public void windowGainedFocus(WindowEvent arg0) {
				// TODO Auto-generated method stub
				toFront();
			}

			@Override
			public void windowLostFocus(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		};
}
